
package com.compuvest.web;

// <editor-fold defaultstate="collapsed" desc="import">
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// </editor-fold>

public class SessionTrack implements Filter {

    public static final Logger log = LoggerFactory.getLogger(SessionTrack.class);

    private final static String VERSION = "SessionTrack v2.0 2008-02-08";
//    private final static String FILTER_APPLIED = "_filter_applied"; // this need only for admin page
    private final static String COOKIE_APPLIED = "_cookie_applied";

    private static boolean trackEnable;

    @Override
    public void init(FilterConfig config) throws ServletException {

        trackEnable = config.getInitParameter("tracking") == null ? false : "true".equals(config.getInitParameter("tracking"));

        log.info("Init: " + VERSION + " track=" + trackEnable);
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain nextFilter) throws IOException {

        try {
            HttpSession session = ((HttpServletRequest)request).getSession(true);
            Basket basket = (Basket)session.getAttribute("basket");
            // set cookies
            basket.getClickstream().addRequest((HttpServletRequest)request);

            if(session.getAttribute(COOKIE_APPLIED) == null) { // one time
                session.setAttribute(COOKIE_APPLIED, Boolean.TRUE);

                ServletContext context = ((HttpServletRequest)request).getSession().getServletContext();

                basket.setCookies(new CookieBean(((HttpServletRequest)request), (HttpServletResponse)response));
            }

//            String name = "", query = "";
//            if(request instanceof HttpServletRequest) {
//                name = ((HttpServletRequest)request).getRequestURI();
//                query = ((HttpServletRequest)request).getQueryString();
//            }
//            System.out.println("\t[" + name + "?" + query + "]");
//	    long startTime = System.currentTimeMillis();

            nextFilter.doFilter(request, response);

//	    long doTime = System.currentTimeMillis() - startTime;
//            System.out.println("========" + (doTime) + "ms");
/*
	    // find long select
	    if((doTime) > 400L) {
		String name = "";
		String query = "";
		if(request instanceof HttpServletRequest) {
		    name = ((HttpServletRequest)request).getRequestURI();
		    query = ((HttpServletRequest)request).getQueryString();
		}
		System.out.println("\t" + name + "?" + query + ": " + (doTime) + "ms");
	    }
*/
	}
	catch(ServletException se) {
            log.error("doFilter(): " + se);
        }
    }

    @Override
    public void destroy() {
	log.info("Destroy: " + VERSION);
    }
}
